package com.pai.gui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.ScrollPaneConstants;

import com.pai.gui.CustomScrollBarUI;

import com.pai.entity.MyInfo;
import com.pai.entity.PaiTask;
import com.pai.entity.User;
import com.pai.gui.component.DownloadTaskCell;
import com.pai.gui.component.DownloadTaskList;
import com.pai.gui.component.DownloadTaskListModel;
import com.pai.gui.component.UserIconList;
import com.pai.gui.component.UserListCell;
import com.pai.gui.component.UserListModel;
import com.pai.gui.component.WorkingTaskCell;
import com.pai.gui.component.WorkingTaskList;
import com.pai.gui.component.WorkingTaskListModel;
import com.pai.service.BroadCastService;
import com.pai.service.TransFileManager;
import com.pai.util.XMLUtil;
/**
 * 这是主界面的设置和布局
 * @author Vido(main)+Jeff+zhang
 *
 */
public class MainFrame extends TranslucentWindow {
	//panel
	private JPanel MainPane;
	
	//left 列表成员变量
	private UserIconList iconlist;
	private WorkingTaskListModel listsource = new WorkingTaskListModel();
	
	//middel 列表成员变量
	private WorkingTaskList tasklist;
	
	//right 列表成员变量
	private DownloadTaskList downlist = new DownloadTaskList();
	private DownloadTaskListModel  rightlistsource = new DownloadTaskListModel();
	
	//文件传输管理线程
	private TransFileManager tfm;
	
	/**
	 * 主界面的构造函数
	 */
	public MainFrame(){
		this.setBounds(250, 80, 800, 600);
			
		//设置主界面大背景
		MainPane = new JPanel() { 
	        @Override 
	        public void paintComponent(Graphics g) { 
	        	super.paintComponent(g);
	            g.drawImage(new ImageIcon("res/icon/MainFrameIcon/MainFrameBackground.png").getImage(), 0, 0, 800, 600, this);
	            //super.paint(g); 
	        } 
	    };
	    
	    MainPane.setLayout(null);//自定义布局
	    
	    //文件传输控制组件
	    tfm=new TransFileManager(MainPane , listsource,rightlistsource);
        tfm.start();
	    
	    
        //左栏

	    JPanel left = new JPanel();
	    left.setLayout(null);
	    left.setOpaque(false);    
	    List<User> userlist = new ArrayList<User>();
	    UserListModel userlistmodel = new UserListModel();
	    userlistmodel.setSources(userlist);
	    //设置用户列表
	    iconlist = new UserIconList();
	    iconlist.setCell(new UserListCell(tfm));
	    iconlist.setUsersource(userlistmodel);
	    iconlist.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        iconlist.setLocation(0, 0);
        iconlist.setSize(new Dimension(199,800));
        iconlist.setPreferredSize(new Dimension(199,0));
        left.setPreferredSize(new Dimension(199,900));
	    left.add(iconlist);
	    
	    //将上面的JPanel放入滑动栏
	    JScrollPane jsp = new JScrollPane(left);
	    jsp.setOpaque(false);
	    jsp.getViewport().setOpaque(false);
	    jsp.setBounds(0,130,199,470);
	    jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jsp.getVerticalScrollBar().setUI(new CustomScrollBarUI());
        jsp.setBorder(null);
        jsp.getVerticalScrollBar().setUnitIncrement(3);
        jsp.getVerticalScrollBar().setPreferredSize(new Dimension(0, Integer.MAX_VALUE));
        //添加滑动栏进MainFrame
	    MainPane.add(jsp);
	    
	    
	 	//右栏
	    JPanel right = new JPanel();
	    right.setLayout(null);
	    right.setOpaque(false);
	    
		//从xml文件里读取已经有的记录。目前仅读取downloadTask。关闭软件前的workingtask会自动保存为破损态的downloadtask。
		List<PaiTask> downloadtask = (List<PaiTask>)XMLUtil.getListFromXml("res/history/downloadTask.xml");
	    rightlistsource.setSources(downloadtask);
	    downlist.setCell(new DownloadTaskCell());
	    downlist.setDownloadsource(rightlistsource);
	    downlist.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
	    downlist.setLocation(0, 0);
	    downlist.setSize(new Dimension(199,800));
	    downlist.setPreferredSize(new Dimension(199,0));
	    right.add(downlist);
	    
	    right.setPreferredSize(new Dimension(199,600));
	    
	    JScrollPane jsp_R = new JScrollPane(right);
	    jsp_R.setOpaque(false);
	    jsp_R.getViewport().setOpaque(false);
	    jsp_R.setBounds(601, 130, 200, 470);
	    jsp_R.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
	    jsp_R.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
	    jsp_R.getVerticalScrollBar().setUI(new CustomScrollBarUI());
	    jsp_R.setBorder(null);
	    jsp_R.getVerticalScrollBar().setUnitIncrement(3);
	    jsp_R.getVerticalScrollBar().setPreferredSize(new Dimension(0, Integer.MAX_VALUE));
	    
	   
	  //Middle Column
	    JPanel middle = new JPanel();
	    middle.setLayout(null);
	    middle.setOpaque(false);
	    
	    List<PaiTask> list = new ArrayList<PaiTask>();
		listsource.setSources(list);
		tasklist = new WorkingTaskList();
		tasklist.setCell(new WorkingTaskCell());
		tasklist.setWorkingsource(listsource);
		tasklist.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
		tasklist.setBounds(0, 0, 400, 470);
		middle.add(tasklist);
		middle.setBounds(200, 130, 400, 470);
	    

        
        
		//关闭按钮
		Icon exitIcon = new ImageIcon("res/icon/MainFrameIcon/MainFrameExitButton.png");
		JButton Exit = new JButton(exitIcon);
		Exit.setBounds(775, 0, 25, 23);
		Exit.setFocusPainted(false);
		Exit.setBorder(BorderFactory.createEmptyBorder());
		
		Exit.addActionListener(new ActionListener() {	
			@Override
			public void actionPerformed(ActionEvent e) {	
				System.out.println("quit");
				//将已下载的任务保存到xml文件里，从而实现持久化
				
				//对temp文件进行保存
				tfm.quitsave();
				XMLUtil.saveListToXml("res/history/downloadTask.xml", rightlistsource.getAllCell());
				System.exit(0);
			}
		});
		Exit.setRolloverIcon(new ImageIcon("res/icon/MainFrameIcon/MainFrameExitButton2.png"));
		Exit.setPressedIcon(new ImageIcon("res/icon/MainFrameIcon/MainFrameExitButton3.png"));
	    
		//标题栏
		Icon Title = new ImageIcon("res/icon/MainFrameIcon/MainFrameTitle.png");
		JButton title = new JButton(Title);
		title.setBounds(0, 100, 800, 30);
		title.setBorder(BorderFactory.createEmptyBorder());
	    
		//功能栏（未添加）
		JPanel FuncBox = new JPanel();
		JLabel icon = new JLabel(new ImageIcon("res/icon/little_head.png"));
		JLabel name = new JLabel(MyInfo.me.getUsername());
		FuncBox.setLayout(null);
		FuncBox.setBackground(new Color(10,10,10));
		name.setFont(new Font(/*"Adobe 黑体 Std"*/"Arial",Font.PLAIN,18));
		name.setForeground(Color.white);
	    icon.setBounds(0, 0, 50, 50);
	    name.setBounds(70, 0, 300, 50);
	    FuncBox.setBounds(0, 40, 800, 60);
		FuncBox.add(icon);
		FuncBox.add(name);

	    //MainPane.add(left);
	    MainPane.add(jsp_R);
	    MainPane.add(middle);
	    MainPane.add(Exit);
	    //MainPane.add(title);
	    MainPane.add(FuncBox);

	    //开启UDP的接收线程。
	    try {
        	System.out.println("UDP receiver start");
			BroadCastService.myservice.receiveMessage(userlistmodel);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	    
	    //添加心跳线程，定时发送广播。
	    System.out.println("UDP heart beat start");
		BroadCastService.myservice.heartBeatMessage(BroadCastService.userFormat(MyInfo.me, 0),userlistmodel);
	    
	    
	    this.setContentPane(MainPane); 
	    this.setVisible(true);
	    
	    
	}
	
	
}
